package com.poobo.util.str;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Random;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StrUtil
{

	private static String varStr = "@";

	public StrUtil()
	{
	}

	public static String replaceAllForTemplet(String str, String templetStr, String data)
	{
		String s = replaceAll(str, (new StringBuilder(String.valueOf(varStr))).append(templetStr).toString(), data);
		return s;
	}

	public static int getNotNullIntValue(String data)
	{
		int value = 0;
		try
		{
			value = Integer.parseInt(data);
		} catch (NumberFormatException e)
		{
			value = 0;
		}
		return value;
	}

	public static int getNotNullIntValue(String date, int returnValue)
	{
		int value = getNotNullIntValue(date);
		if (value == 0)
			value = returnValue;
		return value;
	}

	public static Timestamp getNotNullTimestampValue(String data, String dateFormat)
	{
		Timestamp value;
		try
		{
			if (data == null || data.equals(""))
			{
				value = new Timestamp(System.currentTimeMillis());
			} else
			{
				SimpleDateFormat smd = new SimpleDateFormat(dateFormat);
				value = new Timestamp(smd.parse(data).getTime());
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			value = new Timestamp(System.currentTimeMillis());
		}
		return value;
	}

	public static String getNotNullStringValue(String date, String returnValue)
	{
		String value = date;
		if (date == null || date.trim().equals(""))
			value = returnValue;
		return value;
	}

	public static String getNotNullStringValue(String date)
	{
		String value = date;
		if (date == null || date.trim().equals(""))
			return "";
		else
			return value;
	}

	public static boolean isNumber(String str)
	{
		boolean isNumber = true;
		int tmp = Integer.parseInt(str.trim());
		NumberFormatException e;
		if (tmp < 0)
			return false;
		else
			return isNumber;
	}

	public static boolean isFloat(String str)
	{
		boolean isFloat = true;
		float tmp = Float.parseFloat(str.trim());
		NumberFormatException e;
		if (tmp < 0.0F)
			return false;
		else
			return isFloat;
	}

	public static String replaceAll(String source, String oldString, String newString)
	{
		if (oldString == null || oldString.equals(""))
			return source;
		if (isNull(source))
			return "";
		StringBuffer output = new StringBuffer();
		int lengOfsource = source.length();
		int lengOfold = oldString.length();
		int posStart;
		int pos;
		for (posStart = 0; (pos = source.indexOf(oldString, posStart)) >= 0; posStart = pos + lengOfold)
		{
			output.append(source.substring(posStart, pos));
			output.append(newString);
		}

		if (posStart < lengOfsource)
			output.append(source.substring(posStart));
		return output.toString();
	}

	public static String replaceAllSpace(String source)
	{
		return replaceAll(source, " ", "");
	}

	public static boolean checkStringRule(String source)
	{
		boolean isOK = true;
		if (source == null || source.trim().equals(""))
			return true;
		if (source.indexOf("<") > -1 || source.indexOf(">") > -1 || source.indexOf(".") > -1 || source.indexOf("\"") > -1 || source.indexOf("'") > -1 || source.indexOf(",") > -1 || source.indexOf("$") > -1 || source.indexOf("#") > -1 || source.indexOf("%") > -1 || source.indexOf("!") > -1 || source.indexOf("~") > -1 || source.indexOf("^") > -1 || source.indexOf("&") > -1 || source.indexOf("*") > -1 || source.indexOf("(") > -1 || source.indexOf(")") > -1 || source.indexOf("=") > -1 || source.indexOf("+") > -1 || source.indexOf("|") > -1 || source.indexOf("\\") > -1 || source.indexOf(";") > -1 || source.indexOf(":") > -1 || source.indexOf("?") > -1 || source.indexOf("��") > -1)
			isOK = false;
		return isOK;
	}

	public static String encodeHTML(String str)
	{
		if (str == null)
		{
			return str;
		} else
		{
			String s = str;
			s = replaceAll(s, "\"", "&quot;");
			s = replaceAll(s, "<", "&lt;");
			s = replaceAll(s, ">", "&gt;");
			s = replaceAll(s, "'", "&#39;");
			return s;
		}
	}

	public static String decodeHTML(String str)
	{
		if (isNull(str))
		{
			return "";
		} else
		{
			String s = str;
			s = replaceAll(s, "&quot;", "\"");
			s = replaceAll(s, "&lt;", "<");
			s = replaceAll(s, "&gt;", ">");
			return s;
		}
	}

	public static Vector strToVector(String str)
	{
		Vector v = new Vector();
		if (str == null || str.trim().equals(""))
			return v;
		String str_array[] = str.split("\\,");
		for (int i = 0; i < str_array.length; i++)
		{
			String tmp = str_array[i].trim().toLowerCase();
			if (tmp != null && !tmp.equals(""))
				v.add(tmp);
		}

		return v;
	}

	public static String strArrayToStr(String strArray[], String splitStr)
	{
		String str = "";
		if (strArray == null || strArray.length <= 0)
			return "";
		for (int i = 0; i < strArray.length; i++)
			if (str.length() > 0)
				str = (new StringBuilder(String.valueOf(str))).append(",").append(strArray[i]).toString();
			else
				str = strArray[i];

		return str;
	}

	public static Vector strArrayToVector(String str_array[])
	{
		Vector v = new Vector();
		if (str_array == null || str_array.length == 0)
			return v;
		for (int i = 0; i < str_array.length; i++)
		{
			String tmp = str_array[i].trim();
			if (tmp != null && !tmp.equals(""))
				v.add(tmp);
		}

		return v;
	}

	public static Vector vectorUnite(Vector v1, Vector v2)
	{
		Vector over = new Vector();
		over.addAll(v1);
		over.addAll(v2);
		return over;
	}

	public static Collection mapToCollection(HashMap map)
	{
		return map.values();
	}

	public static boolean isNull(String str)
	{
		return str == null || str.trim().equals("");
	}

	public static boolean isNotNull(String str)
	{
		return str != null && !str.trim().equals("");
	}

	public static boolean isCompareStr(String str)
	{
		boolean b = false;
		if (isNull(str))
			return false;
		if (str.equals("<") || str.equals(">") || str.equals(">=") || str.equals("<=") || str.equals("=") || str.equals("=="))
			b = true;
		return b;
	}

	public static String removeLawlessStr(String str)
	{
		if (str == null || str.trim().equals(""))
		{
			return str;
		} else
		{
			String overStr = str;
			overStr = replaceAll(overStr, "'", "");
			overStr = replaceAll(overStr, "%", "");
			overStr = replaceAll(overStr, "SELECT", "[SELECT]");
			overStr = replaceAll(overStr, "UPDATE", "[UPDATE]");
			overStr = replaceAll(overStr, "DELETE", "[DELETE]");
			return overStr;
		}
	}

	public static String removeHTMLTAG(String str)
	{
		StringBuffer content = new StringBuffer(str);
		do
		{
			int beginHtml = content.indexOf("<");
			int endHtml = content.indexOf(">");
			if (beginHtml >= 0 && endHtml >= 0 && beginHtml < endHtml)
				content = content.replace(beginHtml, endHtml + 1, "");
			else
				return content.toString();
		} while (true);
	}

	public static String encodeHTMLAndRemoveLawlessStr(String str)
	{
		String s = encodeHTML(str);
		return removeLawlessStr(s);
	}

	public static ArrayList split(String str, char c)
	{
		if (str == null)
			return null;
		ArrayList tmp = new ArrayList();
		String temp = null;
		int kk = 0;
		int tt = 0;
		for (int i = 0; i < str.length(); i++)
			if (str.charAt(i) == c)
			{
				kk = i;
				temp = str.substring(tt, kk);
				tmp.add(temp);
				tt = i + 1;
			}

		if (tt < str.length())
			tmp.add(str.substring(tt));
		if (tmp.size() <= 0)
			return null;
		else
			return tmp;
	}

	public static String vectorToStr(Vector v)
	{
		String s = "";
		for (Enumeration e = v.elements(); e.hasMoreElements();)
		{
			String tmp = (String) e.nextElement();
			if (s.length() > 0)
				s = (new StringBuilder(String.valueOf(s))).append(",").append(tmp).toString();
			else
				s = tmp;
		}

		return s;
	}

	public static String substring(String str, int toCount, String more)
	{
		int reInt = 0;
		String reStr = "";
		if (str == null)
			return "";
		char tempChar[] = str.toCharArray();
		for (int kk = 0; kk < tempChar.length && toCount > reInt; kk++)
		{
			String s1 = String.valueOf(tempChar[kk]);
			byte b[] = s1.getBytes();
			reInt += b.length;
			reStr = (new StringBuilder(String.valueOf(reStr))).append(tempChar[kk]).toString();
		}

		if (toCount == reInt || toCount == reInt - 1)
			reStr = (new StringBuilder(String.valueOf(reStr))).append(more).toString();
		return reStr;
	}

	public static int getLength(String str)
	{
		int length = str.length();
		try
		{
			length = str.getBytes("GBK").length;
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		return length;
	}

	public static boolean isHasInArray(String array[], String str)
	{
		boolean is = false;
		for (int i = 0; i < array.length; i++)
			if (array[i].equalsIgnoreCase(str))
				return true;

		return is;
	}

	public static String getMapKeyStr(HashMap map)
	{
		Iterator it = map.keySet().iterator();
		String over = "";
		while (it.hasNext())
		{
			String key = (String) it.next();
			if (over.equals(""))
				over = key;
			else
				over = (new StringBuilder(String.valueOf(over))).append(",").append(key).toString();
		}
		return over;
	}

	public static String getMapValueStr(HashMap map)
	{
		Iterator it = map.values().iterator();
		String over = "";
		while (it.hasNext())
		{
			String value = (String) it.next();
			if (over.equals(""))
				over = value;
			else
				over = (new StringBuilder(String.valueOf(over))).append(",").append(value).toString();
		}
		return over;
	}

	public static String encodeSqlStr(String str)
	{
		if (isNull(str))
		{
			return "";
		} else
		{
			String s = str;
			s = replaceAll(s, "'", "\\'");
			return s;
		}
	}

	public static boolean isChinese(String source)
	{
		boolean retPar = false;
		String regex = "[^u4E00-u9FA5]+";
		Pattern pattern = Pattern.compile(regex, 34);
		Matcher matcher = pattern.matcher(source);
		if (matcher.find())
			retPar = true;
		return retPar;
	}

	public static float getNotNullFloatValue(String data)
	{
		float value = 0.0F;
		try
		{
			value = Float.parseFloat(data);
		} catch (NumberFormatException e)
		{
			value = 0.0F;
		}
		return value;
	}

	public static float getNotNullFloatValue(String date, float returnValue)
	{
		float value = getNotNullFloatValue(date);
		if (value == 0.0F)
			value = returnValue;
		return value;
	}

	public static String getNullValueStr(String str)
	{
		if (isNull(str))
			return " ";
		else
			return str;
	}

	public static double getNotNullDoubleValue(String data)
	{
		double value = 0.0D;
		try
		{
			value = Double.parseDouble(data);
		} catch (NumberFormatException e)
		{
			value = 0.0D;
		}
		return value;
	}

	public static double getNotNullDoubleValue(String date, double returnValue)
	{
		double value = getNotNullDoubleValue(date);
		if (value == 0.0D)
			value = returnValue;
		return value;
	}

	public static String getMiddleStr(String targetStr, String startStr, String endStr, boolean isBlur)
	{
		String returnStr = targetStr;
		int startIndex = -1;
		if (isNotNull(startStr))
			startIndex = returnStr.indexOf(startStr);
		int endIndex = -1;
		if (isNotNull(endStr))
			endIndex = returnStr.indexOf(endStr);
		if (!isBlur)
		{
			if (startIndex < 0 || endIndex < 0)
				return "";
			if (startIndex > endIndex)
			{
				returnStr = returnStr.substring(startIndex + startStr.length(), returnStr.length());
				endIndex = returnStr.indexOf(endStr);
				if (endIndex < 0)
					return "";
				returnStr = returnStr.substring(0, endIndex);
			} else
			{
				returnStr = returnStr.substring(startIndex + startStr.length(), endIndex);
			}
		} else
		{
			if (startIndex > endIndex)
				return "";
			if (startIndex > -1)
				returnStr = returnStr.substring(startIndex + startStr.length(), returnStr.length());
			if (endIndex > -1)
				returnStr = returnStr.substring(0, endIndex);
		}
		return returnStr;
	}

	public static String getMiddleStr(String targetStr, String startStr, String endStr)
	{
		return getMiddleStr(targetStr, startStr, endStr, false);
	}

	public static String getMapValueStrForExt(HashMap map)
	{
		Iterator it = map.values().iterator();
		String over = "";
		while (it.hasNext())
		{
			String value = (String) it.next();
			if (over.equals(""))
				over = value;
			else
				over = (new StringBuilder(String.valueOf(over))).append("@;@").append(value).toString();
		}
		return over;
	}

	public static long getNotNullLongValue(String data)
	{
		long value = 0L;
		try
		{
			value = Long.parseLong(data);
		} catch (NumberFormatException e)
		{
			value = 0L;
		}
		return value;
	}

	public static long getNotNullLongValue(String date, long returnValue)
	{
		long value = getNotNullLongValue(date);
		if (value == 0L)
			value = returnValue;
		return value;
	}

	public static String decodeURI(String target, String enc)
	{
		String returnStr = "";
		try
		{
			returnStr = URLDecoder.decode(target, enc);
		} catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
			return "";
		}
		return returnStr;
	}

	public static String strCollToStr(Collection strCollection, String splitStr)
	{
		String str = "";
		if (strCollection == null || strCollection.size() <= 0)
			return "";
		for (Iterator it = strCollection.iterator(); it.hasNext();)
		{
			String value = (String) it.next();
			if (str.length() > 0)
				str = (new StringBuilder(String.valueOf(str))).append(",").append(value).toString();
			else
				str = value;
		}

		return str;
	}

	public static String addZeroAtNumHead(String value)
	{
		return addZeroAtNumHead(value, 12);
	}

	public static String addZeroAtNumHead(String value, int length)
	{
		if (isNull(value))
			return "";
		int intLength = value.indexOf(".");
		if (intLength > 0)
		{
			String str1 = value.substring(0, intLength);
			String str2 = value.substring(intLength + 1);
			if (isNull(str2))
				str2 = "00";
			if (str2.length() == 1)
				str2 = (new StringBuilder(String.valueOf(str2.substring(0, 1)))).append("0").toString();
			else
				str2 = str2.substring(0, 2);
			if (length < intLength)
				return value;
			int cha = length - intLength;
			String zero = "";
			for (int i = 0; i < cha; i++)
				zero = (new StringBuilder(String.valueOf(zero))).append("0").toString();

			return (new StringBuilder(String.valueOf(zero))).append(str1).append(str2).toString();
		}
		int valueLength = value.length();
		if (length < valueLength)
			return value;
		int cha = length - valueLength;
		String zero = "";
		for (int i = 0; i < cha; i++)
			zero = (new StringBuilder(String.valueOf(zero))).append("0").toString();

		return (new StringBuilder(String.valueOf(zero))).append(value).append("00").toString();
	}

	public static String addPoint(String value)
	{
		return addPoint(value, 2);
	}

	public static String addPoint(String value, int jd)
	{
		if (isNull(value))
			return "";
		if (value.indexOf(".") >= 0)
			return value;
		int length = value.length();
		if (length < jd)
		{
			return value;
		} else
		{
			String returnStr = (new StringBuilder(String.valueOf(value.substring(0, length - jd)))).append(".").append(value.substring(length - jd)).toString();
			return returnStr;
		}
	}

	public static boolean isEmpty(String str) {
		if (str == null || "".equals(str.trim())) {
			return true;
		}
		return false;
	}

	public static String getRandomNum(int max) {
		int rd = new Random().nextInt(max);
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < (max + "").length() - 1 - (rd + "").length(); i++) {
			sb.append("0");
		}
		return sb.toString() + rd;
	}

}
